package com.diwu.diwuoj.judge.codesandbox.impl;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.diwu.diwuoj.common.ErrorCode;
import com.diwu.diwuoj.exception.BusinessException;
import com.diwu.diwuoj.judge.codesandbox.CodeSandbox;
import com.diwu.diwuoj.judge.codesandbox.model.ExecuteCodeRequest;
import com.diwu.diwuoj.judge.codesandbox.model.ExecuteCodeResponse;
import org.apache.commons.lang3.StringUtils;

/**
 * @Description 远程的代码沙箱
 * @Author diwu
 * @Date 2024/7/1
 */
public class RemoteCodeSandbox implements CodeSandbox {
    @Override
    public ExecuteCodeResponse executeCode(ExecuteCodeRequest executeCodeRequest) {
        System.out.println("远程的代码沙箱");
        String url = "http://localhost:8089/executeCode";
        String jsonStr = JSONUtil.toJsonStr(executeCodeRequest);
        String responseStr = HttpUtil.createPost(url)
                .body(jsonStr)
                .execute()
                .body();
        //这里返回的是Json的字符串
        if(StringUtils.isBlank(responseStr)){
            throw new BusinessException(ErrorCode.API_REQUEST_ERROR,"API 请求失败");
        }
        return JSONUtil.toBean(responseStr, ExecuteCodeResponse.class);
    }
}
